/*
 * TermSequentialFile_gtest.h
 *
 *  Created on: Nov 27, 2012
 *      Author: bruno
 */

#ifndef INVERTEDLISTFILETEST_GTEST_H_
#define INVERTEDLISTFILETEST_GTEST_H_

#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include "../physical_layer/file_system/InvertedListFile.h"

using physical::InvertedListFile;
using physical::InvertedList;

/**
 * Test Suite de la clase TermSequentialFile
 */
class InvertedListFileTestFixture: public ::testing::Test {

public:

	std::string m_filename;

	InvertedListFileTestFixture() {
		const ::testing::TestInfo* const test_info =
				::testing::UnitTest::GetInstance()->current_test_info();

		std::string filename(test_info->name());
		filename.append(".");
		filename.append(test_info->test_case_name());
		filename.append(".tmp");

		m_filename = filename;
	}

	virtual ~InvertedListFileTestFixture() {
	}

	void SetUp() {
	}

	void TearDown() {
	}

	const char* getFileNameForTest() {
		return m_filename.c_str();
	}
};

/**
 * Test Add and retrieve one inverted list of the inverted list file
 *
 *
 */
TEST_F( InvertedListFileTestFixture, PutAndGetInvertedList) {

	 InvertedList invertedList;
	 size_t blockId;

	 {
		 InvertedListFile invertedListFile(getFileNameForTest(), true);

		 invertedList = { 1, 2, 3, 4, 5 };

		 blockId = invertedListFile.addList(4, invertedList);
	 }
	 {
		 InvertedListFile invertedListFile(getFileNameForTest(), false);

		 InvertedList retrievedList = invertedListFile.getList(4, blockId);

		 ASSERT_TRUE(retrievedList == invertedList);
	 }
}

/**
 * Test Add and retrieve several inverted list of the inverted list file
 *
 *
 */
TEST_F( InvertedListFileTestFixture, PutAndGetSeveralInvertedLists) {

	 InvertedList invertedList = { 1, 2, 3, 4, 5 };

	 InvertedList invertedList2 = { 6, 7, 8, 9, 10 };

	 InvertedList invertedList3 = { 11, 12, 13, 14, 15 };

	 size_t blockId;

	 size_t blockId2;

	 size_t blockId3;

	 {

		 InvertedListFile invertedListFile(getFileNameForTest(), true);

		 blockId = invertedListFile.addList(4, invertedList);

		 blockId2 = invertedListFile.addList(3, invertedList);

		 blockId3 = invertedListFile.addList(8, invertedList);

	 }

	 {
		 InvertedListFile invertedListFile(getFileNameForTest(), false);

		 InvertedList retrievedList = invertedListFile.getList(4, blockId);

		 ASSERT_TRUE(retrievedList == invertedList);

		 retrievedList = invertedListFile.getList(8, blockId3);

		 ASSERT_TRUE(retrievedList == invertedList);

		 retrievedList = invertedListFile.getList(3, blockId2);

		 ASSERT_TRUE(retrievedList == invertedList);

	 }
}

#endif /*INVERTEDLISTFILETEST_GTEST_H_*/
